package com.yunfei.commonbase.base.entity;

import com.yunfei.commonbase.generationcode.code.CreateCode;

import java.lang.reflect.Field;
import java.util.Iterator;

public class Example {
    private Class relation;
    private StringBuilder condition;
    private String orderBy;

    public Example(Class relation) {
        this.relation = relation;
    }

    public Example equals(String colimn, Object value) throws NoSuchFieldException {
        setColumnValueOne(colimn, value, "=", false);
        return this;
    }

    public Example notEquals(String column, Object value) throws NoSuchFieldException {
        setColumnValueOne(column, value, "!=", false);
        return this;
    }

    public Example like(String column, Object value) throws NoSuchFieldException {
        setColumnValueOne(column, value, null, true);
        return this;
    }

    public Example isNull(String column) throws NoSuchFieldException {
        setColumnValueNull(column, " is null");
        return this;
    }

    public Example isNotNull(String column) throws NoSuchFieldException {
        setColumnValueNull(column, " is not null");
        return this;
    }

    public Example in(String column, Iterable value) throws Exception {
        setColumnValueList(column, value, " in");
        return this;
    }

    public Example notIn(String column, Iterable value) throws Exception {
        setColumnValueList(column, value, " not in");
        return this;
    }

    public void setColumnValueOne(String column, Object value, String symbol, boolean like) throws NoSuchFieldException {
        boolean check = false;
        String strCheck = "";
        try {
            Field declareField = relation.getDeclaredField(column);
            check = true;
            if (declareField.getType().getName().equals("java.lang.String")) {
                strCheck = "";
            }
        } catch (Exception e) {
            Field declareField = relation.getSuperclass().getDeclaredField(column);
            check = true;
            if (declareField.getType().getName().equals("java.lang.String")) {
                strCheck = "";
            }
        }
        if (check) {
            column = CreateCode.camelToUnderline(column);
            if (condition != null) {
                if (like) {
                    condition.append(" and ").append(column).append(" like ").append(" ('%").append(value).append("%')");
                } else {
                    condition.append(" and ").append(column).append(" ").append(symbol).append(strCheck).append(value).append(strCheck);
                }
            }else{
                if (like) {
                    condition = new StringBuilder(" ").append(column).append(" like").append(" ('%").append(value).append("%')");
                } else {
                    condition = new StringBuilder(" ").append(column).append(" ").append(symbol).append(" ").append(strCheck).append(value).append(strCheck);
                }
            }
        }
    }

    public void setColumnValueNull(String column, String symbol) throws NoSuchFieldException {
        boolean check = false;
        String strCheck = "";
        try {
            Field declaredField = relation.getDeclaredField(column);
            check = true;
            if (declaredField.getType().getName().equals("java.lang.String")) {
                strCheck = "'";
            }
        } catch (Exception e) {
            Field declaredField = relation.getSuperclass().getDeclaredField(column);
            check = true;
            if (declaredField.getType().getName().equals("java.lang.String")) {
                strCheck = "";
            }
        }
        if (check) {
            column = CreateCode.camelToUnderline(column);
            if (condition != null) {
                condition.append(" and ").append(column).append(" ").append(symbol);
            } else {
                condition = new StringBuilder(" ").append(column).append(" ").append(symbol);
            }
        }
    }

    private void setColumnValueList(String column, Iterable value, String symbol) throws Exception {
        boolean check = false;
        String strCheck = "";
        try {
            Field declaredField = relation.getDeclaredField(column);
            check = true;
            if (declaredField.getType().getName().equals("java.lang.String")) {
                strCheck = "'";
            }
        } catch (NoSuchFieldException e) {
            Field declaredField = relation.getSuperclass().getDeclaredField(column);
            check = true;
            if (declaredField.getType().getName().equals("java.lang.String")) {
                strCheck = "'";
            }
        }
        if (check) {
            column = CreateCode.camelToUnderline(column);
            StringBuilder val = new StringBuilder();
            Iterator iterator = value.iterator();
            boolean hasNext = iterator.hasNext(); //逗号的判断  比截取字符串应该快点
            while (hasNext) {
                val.append(strCheck).append(iterator.next()).append(strCheck);
                hasNext = iterator.hasNext();
                if (hasNext)
                    val.append(",");
            }
            if (condition != null) {
                condition.append(" and ").append(column).append(" ").append(symbol).append(" (").append(val).append(")");
            } else {
                condition = new StringBuilder(" ").append(column).append(" ").append(symbol).append(" (").append(val).append(")");
            }
        }
    }

    public Class getRelation() {
        return relation;
    }

    public void setRelation(Class relation) {
        this.relation = relation;
    }

    public StringBuilder getCondition() {
        return condition;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }
}
